package com.hare.harelearn.DataStructure.structure.栈存储结构.栈链式存储结构;

import java.util.Stack;

/**
 * @program: HareLearn
 * @description:  栈链式存储结构
 * @author: Hare
 * @create: 2025−05-22 17:09
 * @Version 1.0
 **/
public class StackSort {
    // 栈顶指针
    static Node head = new Node(null,null);
    // 栈顶top
    static Node top;

    static {
         Node node0 = new  Node("node0", "java");
         Node node1 = new  Node("node1", "go");
         Node node2 = new  Node("node2", "python");
         Node node3 = new  Node("node3", "vue");
         Node node4 = new  Node("node4", "php");

        if (head.getNext() == null) {
            head.setNext(node0);
        }

        node0.setNext(node1);
        node1.setNext(node2);
        node2.setNext(node3);
        node3.setNext(node4);
        node4.setNext(null);

        // 第一个结点为栈顶
        top = head.getNext();
    }

    public static void main(String[] args) throws Exception {
        //System.out.println(pop());
        Node node5 = new  Node("node5", "js");
        push(node5);
    }

    /**
     * 出栈
     */
    public static Node pop() throws Exception {
        if(!stackEmpty()){
            throw new Exception("栈为空~~~");
        }
        // 通过栈顶指针获取第一个结点
        Node top = StackSort.head.getNext();
        // 将栈顶指针指向下一个结点
        StackSort.top = top.getNext();
        // 将栈顶指针重新指向新的结点
        StackSort.head.setNext(top.getNext());
        System.out.println("新栈顶: "+ StackSort.top);
        return top;
    }

    /**
     * 进栈
     */
    public static void push(Node node) throws Exception {
        Node next = StackSort.head.getNext();
        node.setNext(next);
        StackSort.head.setNext(node);
        StackSort.top = node;
    }

    /**
     * 判断栈是否为空
     * 是否为空即判断top==null即可
     */
    public static boolean stackEmpty() {
        if(StackSort.top == null){
            return false;
        }
        return true;
    }
}
